<?php
declare (strict_types=1);

namespace app\admin\service\site\personal;

use app\admin\logic\site\Personal;
use app\admin\logic\site\PersonalGroup;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

/**
 * 配置接口
 */
class Config extends BaseService
{
    /**
     * @Inject()
     *
     * @var Personal
     */
    protected $personal;

    /**
     * @Inject()
     *
     * @var PersonalGroup
     */
    protected $personalGroup;

    /**
     * @throws DbException
     */
    public function items($type): Response
    {
        $data = $this->personal->getList($type);

        // 定义字段
        $keyList = [
            'title'  => ListItem::make()->title('名称'),
            // 'key'         => ListItem::make()->title('标识'),
            'icon'   => ListItem::make('custom')->title('图标')->visible($type == 'app'),
            // 'group_title' => ListItem::make()->title('组别'),
            'status' => ListItem::make('custom')->title('状态'),
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        // $list->searchKey = $searchKey;
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 编辑元素
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-28
     */
    public function editItem($id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $item = $this->personal->editItem($id, $data);

            return $this->parseToData($item->toArray(), 1, '编辑成功');
        }

        $datas        = $this->personal->with(['icon_url', 'personal_data'])->findOrFail($id);
        $personalData = $datas->personalData;
        if ($personalData) {
            $datas->icon     = $personalData->icon ?: $datas->icon;
            $datas->icon_url = $personalData->icon ? $personalData->icon_url : $datas->icon_url;
            $datas->title    = $personalData->title ?: $datas['title'];
            $datas->status   = $personalData->status ?: 0;
            $datas->sort     = $personalData->sort ?: $datas['sort'];
        }

        // $group_ids = $this->personal
        //     ->where('device_type', $datas->device_type)
        //     ->group('group_id')
        //     ->column('group_id');
        //
        // $group = app(PersonalGroup::class)
        //     ->where('id', 'IN', $group_ids)
        //     ->field(['id' => 'value', 'title'])
        //     ->select()
        //     ->toArray();

        // 定义字段
        $keyList = [
            'title' => FormItem::make()->title('标题')->required(),
            'icon'  => FormItem::make('custom')->title('图标')->required()->visible($datas->getAttr('device_type') == '1'),
            // 'group_id' => FormItem::make('select')->options($group)->title('组别')->disabled()->required(),
            // 'status'   => FormItem::make('slideSwitch')->title('显示状态')->activeText('显示')->activeValue(1)->inactiveText('隐藏')->inactiveValue(0)->required(),
        ];

        // 设置表单
        $form = app(PageForm::class);

        // 表单字段
        $form->datas       = $datas;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/admin/site/personal/items/' . $id;

        return $form->send();

    }

    /**
     * 编辑元素字段
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-28
     */
    public function editItemField(int $id, string $field): Response
    {
        $param = Request::put();
        $item  = $this->personal->editItemField($id, $field, $param);

        return $this->parseToData($item, 1, '编辑成功');
    }

    /**
     * 更新分组名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/11/17
     *
     * @param int $gid
     * @return Response
     */
    public function updatedGroupTitle(int $gid): Response
    {
        $title = Request::put('title');
        $this->personalGroup->updatedTitleById($gid, $title);

        return $this->parseToData([], 1, '编辑成功');
    }

    /**
     * 更新组排序
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/08
     *
     * @param int $gid
     * @return Response
     */
    public function updatedGroupSort(int $gid): Response
    {
        $sort = Request::put('sort');
        $this->personalGroup->updateSortById($gid, $sort);

        return $this->parseToData([], 1, '编辑成功');
    }

    /**
     * 更新组所有菜单状态
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/08
     *
     * @param int $gid
     * @return Response
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function updatedGroupStatus(int $gid): Response
    {
        $param = Request::put();
        $this->personal->updateStatusByGroupId($gid, $param);

        return $this->parseToData([], 1, '编辑成功');
    }

}
